using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Reflection;
using System.Text;
// using System.Web;


namespace Ras2ClassLib.Common.Data
{
	public class DataList<T> : List<T> where T : IDataItem, new()
	{
		public DataList()
		{
		}

		public void OnAddRow(IDataReader reader)
		{
			T t = new T();
			t.Load(reader);
			this.Add(t);
		}

		public String ToCSV(String[] columnsToDisplay)
		{
			StringBuilder strCSV = new StringBuilder();

			if (this.Count > 0)
			{
				// First, build a list of properties which we will include,
				// and build the header from them:
				List<PropertyInfo> propList = new List<PropertyInfo>();
				object firstObj = this[0];
				Type t = firstObj.GetType();

				foreach (String strColName in columnsToDisplay)
				{
					PropertyInfo propInfo = t.GetProperty(strColName);
					propList.Add(propInfo);

					strCSV.Append(strColName);
					strCSV.Append(";");
				}
				strCSV.Append(System.Environment.NewLine);

				// Then foreach item in the list, get the values
				// of the corresponding properties and add a new
				// line with those values.
				foreach (object o in this)
				{
					foreach (PropertyInfo prop in propList)
					{
						object oValue = prop.GetValue(o, null);
						if (oValue != null)
						{
							String strValue = oValue.ToString();
							strCSV.Append(strValue);
						}
						else
						{

						}
						strCSV.Append(";");
					}
					strCSV.Append(System.Environment.NewLine);
				}
			}

			return strCSV.ToString();
		}
	}
}
